export default function model (event = 'input', inputValue = '') {
  return {
    data: () => ({
      inputValue,
    }),

    model: {
      prop: 'value',
      event,
    },

    props: {
      value: [String, Number, Array, Object, Date],
    },

    computed: {
      // value
      isValue () {
        return !!this.inputValue.toString()
      },
    },

    watch: {
      value: {
        immediate: true,
        handler (val) {
          this.inputValue = val
        },
      },
    },

    methods: {
      // 重置内容
      resetValue () {
        if (this.canDo) return this.inputValue
        this.inputValue = ''
        this.toggleEvent(event, this.inputValue)
      },

      // event taggle
      toggleEvent (evt, val) {
        if (this.canDo) return false
        this.$emit(evt, val)
        this.clearRefChild && this.clearRefChild()
      },

      // change
      onChange (val) {
        if (val === this.value) return false

        this.toggleEvent('change', val)

        this.validate()
      },

      // input
      onInput (val) {
        if (val === this.value) return false
        this.toggleEvent('input', val)

        this.validate()
      },
    },
  }
}
